import axios from '../axios'

/*
 * 心理测评成绩管理模块
 */

// 调试输出
console.log('psyScore模块已加载');

// 所有API方法
const psyScore = {
  // 保存
  save(data) {
    console.log('save方法被调用');
    return axios({
      url: '/psyScore/save',
      method: 'post',
      data
    })
  },

  // 删除
  batchDelete(data) {
    console.log('batchDelete方法被调用');
    return axios({
      url: '/psyScore/delete',
      method: 'post',
      data
    })
  },

  // 分页查询
  findPage(data) {
    console.log('findPage方法被调用');
    
    // 确保data参数至少包含基本结构
    const requestData = data || { pageNum: 1, pageSize: 10 };
    
    // 确保params存在,且至少有一个占位参数
    if (!requestData.params || !Array.isArray(requestData.params) || requestData.params.length === 0) {
      requestData.params = [{ name: '_placeholder', value: null }];
    }
    
    return axios({
      url: '/psyScore/findPage',
      method: 'post',
      data: requestData
    })
  },

  // 根据ID查询
  findById(id) {
    console.log('findById方法被调用');
    return axios({
      url: '/psyScore/findById',
      method: 'get',
      params: {
        id: id
      }
    })
  },

  // 获取当前用户的成绩
  findByCurrentUser(termId) {
    console.log('findByCurrentUser方法被调用');
    return axios({
      url: '/psyScore/findByCurrentUser',
      method: 'get',
      params: {
        termId: termId
      }
    })
  },

  // 计算排名
  calculateRanking(params) {
    console.log('calculateRanking方法被调用');
    return axios({
      url: '/psyScore/calculateRanking',
      method: 'post',
      data: params
    })
  },

  // 导出成绩
  exportScores(params) {
    console.log('exportScores方法被调用');
    return axios({
      url: '/psyScore/exportScores',
      method: 'post',
      data: params,
      responseType: 'blob'
    })
  },

  // 根据学期获取成绩
  getScoreByTerm(termId) {
    console.log('getScoreByTerm方法被调用');
    return axios({
      url: '/psyScore/getScoreByTerm',
      method: 'get',
      params: {
        termId: termId
      }
    })
  },

  // 根据学生学号和学期ID获取心理测评成绩
  findByStudentAndTerm(params) {
    console.log('findByStudentAndTerm方法被调用');
    return axios({
      url: '/psyScore/findByStudentAndTerm',
      method: 'get',
      params: params
    })
  }
}

// 导出API对象
export default psyScore

// 为了向下兼容，同时导出各个方法
export const save = psyScore.save
export const batchDelete = psyScore.batchDelete
export const findPage = psyScore.findPage
export const findById = psyScore.findById
export const findByCurrentUser = psyScore.findByCurrentUser
export const calculateRanking = psyScore.calculateRanking
export const exportScores = psyScore.exportScores
export const getScoreByTerm = psyScore.getScoreByTerm
export const findByStudentAndTerm = psyScore.findByStudentAndTerm 